<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Critical</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>Critical</h1>

<p>Prevents the <a href="../misc/Threads.htm">current thread</a> from being interrupted by other threads.</p>

<pre class="Syntax">Critical [, Off]
Critical 50 <em>; See <a href="#Interval">bottom of remarks</a>.</em></pre>
<p>If the first parameter is omitted (or the word On), the <a href="../misc/Threads.htm">current thread</a> is made critical, meaning that it cannot be interrupted by another thread. If the first parameter is the word Off (or the number 0 in v1.0.48+), the current thread immediately becomes interruptible, regardless of the settings of <a href="Thread.htm">Thread Interrupt</a>.</p>
<p>Unlike <a href="../misc/Threads.htm">high-priority</a> threads, events that occur during a critical thread are not discarded. For example, if the user presses a <a href="../Hotkeys.htm">hotkey</a> while the current thread is critical, the hotkey is buffered indefinitely until the current thread finishes or becomes noncritical, at which time the hotkey is launched as a new thread.</p>
<p>A critical thread will be interrupted in emergencies. Emergencies consist of: 1) the <a href="OnExit.htm">OnExit</a> subroutine; 2) any <a href="OnMessage.htm">OnMessage()</a> function that monitors a message number less than 0x312 (or a <a href="RegisterCallback.htm">callback</a> triggered by such a message); and 3) any <a href="RegisterCallback.htm">callback</a> indirectly triggered by the critical thread itself (e.g. via <a href="PostMessage.htm">SendMessage</a> or <a href="DllCall.htm">DllCall</a>). To avoid these interruptions, temporarily disable such functions.</p>
<p>When buffered events are waiting to start new threads, using &quot;Critical Off&quot; will not result in an immediate interruption of the current thread. Instead, an average of 5 milliseconds will pass before an interruption occurs. This makes it more than 99.999% likely that at least one line after &quot;Critical Off&quot; will execute before an interruption. You can force interruptions to occur immediately by means of a delay such as a <code><a href="Sleep.htm">Sleep -1</a></code> or a <a href="WinWait.htm">WinWait</a> for a window that does not yet exist.</p>
<p>A critical thread becomes interruptible when a <a href="MsgBox.htm">MsgBox</a> or other dialog is displayed. However, unlike <a href="Thread.htm">Thread Interrupt</a>, the thread becomes critical again after the user dismisses the dialog.</p>
<p>See <a href="../Variables.htm#IsCritical">A_IsCritical</a> for how to save and restore the current setting of Critical. However, since Critical is a thread-specific setting, when a critical thread ends, the underlying/resumed thread (if any) will  be automatically noncritical. Consequently there is no need to do &quot;Critical Off&quot; right before ending a thread.</p>
<p>If Critical is not used in the auto-execute section (top part of the script), all threads start off as noncritical (though the settings of <a href="Thread.htm">Thread Interrupt</a> will still apply). By contrast, if the auto-execute section turns on Critical but never turns it off, every newly launched <a href="../misc/Threads.htm">thread</a> (such as a <a href="../Hotkeys.htm">hotkey</a>, <a href="Menu.htm">custom menu item</a>, or <a href="SetTimer.htm">timed</a> subroutine) starts off critical.</p>
<p>The command <a href="Thread.htm">Thread NoTimers</a> is similar to Critical except that it only prevents interruptions from <a href="SetTimer.htm">timers</a>.</p>
<p>In v1.0.47+, turning on Critical also puts <a href="SetBatchLines.htm"><code>SetBatchLines -1</code></a> into effect for the <a href="../misc/Threads.htm">current thread</a>.</p>
<p><a name="Interval"></a>In v1.0.47+, specifying a positive number as the first parameter (e.g. <code>Critical 30</code>) turns on Critical but also changes the number of milliseconds between checks of the internal message queue. If unspecified, messages are checked every 16 milliseconds while Critical is On, and every 5 ms while Critical is Off. Increasing the interval postpones the arrival of messages/events, which gives the <a href="../misc/Threads.htm">current thread</a> more time to finish. This reduces the possibility that certain <a href="OnMessage.htm">OnMessage()</a> and <a href="Gui.htm#DefaultWin">GUI events</a> will be lost due to &quot;thread already running&quot;. However, commands that wait such as <a href="Sleep.htm">Sleep</a> and <a href="WinWait.htm">WinWait</a> will check messages regardless of this setting (a workaround is <code>DllCall(&quot;Sleep&quot;, Uint, 500)</code>). Note: Increasing the message-check interval too much may reduce the responsiveness of various events such as <a href="Gui.htm">GUI</a> window repainting.</p>
<h3>Related</h3>
<p><a href="Thread.htm">Thread (command)</a>, <a href="../misc/Threads.htm">Threads</a>, <a href="_MaxThreadsPerHotkey.htm">#MaxThreadsPerHotkey</a>, <a href="_MaxThreadsBuffer.htm">#MaxThreadsBuffer</a>, <a href="OnMessage.htm">OnMessage()</a>, <a href="RegisterCallback.htm">RegisterCallback()</a>, <a href="Hotkey.htm">Hotkey</a>, <a href="Menu.htm">Menu</a>, <a href="SetTimer.htm">SetTimer</a></p>
<h3>Example</h3>
<pre class="NoIndent">#space::  <em>; Win+Space hotkey.</em>
Critical
ToolTip No new threads will launch until after this ToolTip disappears.
Sleep 3000
ToolTip  <em>; Turn off the tip.</em>
return  <em>; Returning from a hotkey subroutine ends the thread. Any underlying thread to be resumed is noncritical by definition.</em></pre>

</body>
</html>
